package com.lavegue.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.ImageIcon;

import com.lavegue.dao.ManageMsgDao;
import com.lavegue.model.ManageMsg;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


/**
 * 
 * @author luoweibin
 * @date:2011-12-19
 * @description
 * 
 */
public class ShuiyinImg extends HttpServlet {

	//session中的key
	public final static String VALID_CODE_KEY = "nbstore_valid_code";
	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("image/jpeg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		HttpSession session = request.getSession();
		// 在内存中创建图象
		
		
		  String imgDir = request.getSession().getServletContext().getRealPath("") + "/random_images/";
		  new File(imgDir).mkdirs();
		  int msgid = Integer.valueOf(request.getParameter("msgid"));
		  ManageMsgDao msgDao = (ManageMsgDao) MyBeanFactory.getBean("manageMsgDao");
		  ManageMsg msg = msgDao.findById(msgid);
		  String targetImg = "";
		  targetImg = msg.getManageMsgDetails().get(0).getPic();
		  if(StringUtil.isEmpty(targetImg)) {
			  targetImg = "/images/default_msg_pic.jpg";
		  }
		  
		  FileInputStream is = new FileInputStream(request.getSession().getServletContext().getRealPath("") + "/" +targetImg);
	      JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
	      BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
	      Graphics g= buffImg.getGraphics();
	      
	      ImageIcon imgold = new ImageIcon(request.getSession().getServletContext().getRealPath("") + "/" +targetImg);
	      int h = imgold.getIconHeight();
	      int w = imgold.getIconWidth();
			BufferedImage image = new BufferedImage(w, h,
					BufferedImage.TYPE_INT_RGB);
			
			if (msg.getLabel() !=0) {
	     //2.jpg是你的小图片的路径
	      ImageIcon imgIcon = null;
	      if (msg.getLabel() == 1) {
	    	  imgIcon = new ImageIcon(request.getSession().getServletContext().getRealPath("") + "/images/hot.gif"); 
	      } else if (msg.getLabel() == 2) {
	    	  imgIcon = new ImageIcon(request.getSession().getServletContext().getRealPath("") + "/images/new.gif"); 
	      }
	     //得到Image对象。
	      Image img = imgIcon.getImage();
	      
	     //将小图片绘到大图片上。5,300 .表示你的小图片在大图片上的位置。
	      g.drawImage(img,w - 35,0,null);
			}
	     //设置颜色。
//	      g.setColor(Color.BLACK);
	     //最后一个参数用来设置字体的大小
//	      Font f = new Font("宋体",Font.BOLD,30);
//	      g.setFont(f);
	     //10,20 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
	      g.dispose();
	      ServletOutputStream responseOutputStream = response.getOutputStream();
	     
	     //创键编码器，用于编码内存中的图象数据。
	      JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(responseOutputStream);
	      en.encode(buffImg);
	     
	      is.close();
	      System.out.println ("合成结束。。。。。。。。");
		
		// 输出图象到页面
		ImageIO.write(image, "JPEG", responseOutputStream);
		// 以下关闭输入流！
		responseOutputStream.flush();
		responseOutputStream.close();
	}

	Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on
	// the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/** Returns a short description of the servlet.
	 */
	public String getServletInfo() {
		return "Short description";
	}
}
